package com.egreen.order.service.impl;


import com.egreen.common.pojo.DTO.order.AddRecyclableNameDTO;
import com.egreen.common.pojo.entity.order.RecyclableName;
import com.egreen.order.dao.RecyclableNameDao;
import com.egreen.order.service.IRecyclableNameService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cloudJiang
 * @since 2022-07-16
 */
@Service
public class RecyclableNameServiceImpl extends ServiceImpl<RecyclableNameDao, RecyclableName> implements IRecyclableNameService {
    @Autowired
    IRecyclableNameService recyclableNameService;
    @Autowired
    RecyclableNameDao recyclableNameDao;

    /**
     * 可回收物品的名称
     * @param addRecyclableNameDTO
     */
    @Override
    public void addRecyclableName(AddRecyclableNameDTO addRecyclableNameDTO) {
        //遍历数据库并且将数据存入List集合中
        List<RecyclableName> recyclableNames = this.list();
        for (int i = 0; i < recyclableNames.size(); i++) {
            //判断在数据库中是否还有这个可回收物品的类型(有:提示有了;没有:增加)
            if (recyclableNames.get(i).getName().equals(addRecyclableNameDTO.getName())){
                //TODO 抛出一个类型重复异常
                System.out.println("已存在");
                return;
            }
        }
        recyclableNameService.save(new RecyclableName(null, addRecyclableNameDTO.getTypeId(),
                addRecyclableNameDTO.getName(), addRecyclableNameDTO.getDescription(),
                addRecyclableNameDTO.getPrice()));
    }

    @Override
    public void removeRecyclableNameDTO(Integer id) {
        recyclableNameDao.deleteById(id);
    }
}
